/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.vraptor.jpessoal.DAO;

import br.com.caelum.vraptor.ioc.Component;
import br.com.vraptor.jpessoal.bean.Perfil;
import br.com.vraptor.jpessoal.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;



@Component
public class PerfilDao {
    
    
    
    
     public List<Perfil> listar(){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction(); 
        Query query = session.createQuery("from Perfil");
        List<Perfil> lista = query.list();
        session.getTransaction().commit();
        
                
        return lista;        
    }

    public Perfil obterPorId(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query query = session.createQuery("from Perfil where id = :id ");
        query.setParameter("id", id);
        Perfil perfil = (Perfil) query.uniqueResult();
        session.getTransaction().commit();
                
        return perfil;
    }
    
    public void salvar(Perfil perfil){
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(perfil);
        session.getTransaction().commit();
        
    }
    
    public void atualizar(Perfil perfil){
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.update(perfil);
        session.getTransaction().commit();
        
    }
    
}
